package com.readbook.action;


import com.readbook.service.JfychService;
import com.readbook.service.ScdhService;
import com.readbook.util.ExcelUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/scdh")
public class scdh {
    @Resource
    private ScdhService scdhService;

    @Resource
    private JfychService jfychservice;

    @RequestMapping("Search_poetry.html")
    @ResponseBody
    public Map<String, Object> Search_poetry(HttpServletRequest request, HttpServletResponse response) {
        String dhmc = "";
        if (request.getParameter("dhmc") != null) {
            dhmc = request.getParameter("dhmc").toString().trim();
        }

        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("dhmc", dhmc);

        List<Map<String, Object>> list1 = scdhService.Search_poetry(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        return rmap;
    }


    @RequestMapping("del_poetry.html")
    @ResponseBody
    public Map<String, Object> del_poetry(HttpServletRequest request, HttpServletResponse response) {
        String pid = request.getParameter("pid").trim();
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("pid", pid);
        Map<String, Object> getm = scdhService.del_poetry(gmap);
        return getm;
    }


    /**
     * @author 牟明艳
     * @comments 诗词添加
     * @time 2019年8月12日11:05:09
     */
    @RequestMapping("add_poetry.html")
    public @ResponseBody
    Map<String, Object> add_poetry(HttpServletRequest req) throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String pid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("pid", pid);

        //诗词大会名称
        String dhmc = "";
        if (req.getParameter("dhmc") != null) {
            dhmc = req.getParameter("dhmc").toString().trim();
        }
        gmap.put("dhmc", dhmc);

        //开始时间
        String kssj = "";
        if (req.getParameter("kssj") != null) {
            kssj = req.getParameter("kssj").toString().trim();
        }
        gmap.put("kssj", kssj);


        //结束时间
        String jssj = "";
        if (req.getParameter("jssj") != null) {
            jssj = req.getParameter("jssj").toString().trim();
        }
        gmap.put("jssj", jssj);

        //状态
        String pstate = "";
        if (req.getParameter("pstate") != null) {
            pstate = req.getParameter("pstate").toString().trim();
        }
        gmap.put("pstate", pstate);

        String delstate = "";
        if (req.getParameter("delstate") != null) {
            delstate = req.getParameter("delstate").toString().trim();
        }
        gmap.put("delstate", delstate);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("addtime", df.format(new Date()));

        //最高积分
        String totalscore = "";
        if (req.getParameter("totalscore") != null) {
            totalscore = req.getParameter("totalscore").toString().trim();
        }
        gmap.put("totalscore", totalscore);

        //最大排名
        String maxpm = "";
        if (req.getParameter("maxpm") != null) {
            maxpm = req.getParameter("maxpm").toString().trim();
        }
        gmap.put("maxpm", maxpm);

        Map<String, Object> rmap = new HashMap<String, Object>();
        int m = scdhService.add_poetry(gmap);
        if (m > 0) {
            //如果该诗词大会开启 则其他诗词大会关闭
            if (pstate.equals("0")) {
                scdhService.close_otherpoetry(gmap);
            }
            rmap.put("result", true);
            rmap.put("msg", "保存成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "添加失败,请联系管理员");
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 诗词修改
     * @time 2019年8月12日11:17:23
     */
    @RequestMapping("edit_poetry.html")
    public @ResponseBody
    Map<String, Object> edit_poetry(HttpServletRequest req) throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String pid = req.getParameter("pid");
        gmap.put("pid", pid);

        //诗词大会名称
        String dhmc = "";
        if (req.getParameter("dhmc") != null) {
            dhmc = req.getParameter("dhmc").toString().trim();
        }
        gmap.put("dhmc", dhmc);

        //开始时间
        String kssj = "";
        if (req.getParameter("kssj") != null) {
            kssj = req.getParameter("kssj").toString().trim();
        }
        gmap.put("kssj", kssj);


        //结束时间
        String jssj = "";
        if (req.getParameter("jssj") != null) {
            jssj = req.getParameter("jssj").toString().trim();
        }
        gmap.put("jssj", jssj);

        //状态
        String pstate = "";
        if (req.getParameter("pstate") != null) {
            pstate = req.getParameter("pstate").toString().trim();
        }
        gmap.put("pstate", pstate);

        String delstate = "";
        if (req.getParameter("delstate") != null) {
            delstate = req.getParameter("delstate").toString().trim();
        }
        gmap.put("delstate", delstate);

        //最高积分
        String totalscore = "";
        if (req.getParameter("totalscore") != null) {
            totalscore = req.getParameter("totalscore").toString().trim();
        }
        gmap.put("totalscore", totalscore);

        //最大排名
        String maxpm = "";
        if (req.getParameter("maxpm") != null) {
            maxpm = req.getParameter("maxpm").toString().trim();
        }
        gmap.put("maxpm", maxpm);


        Map<String, Object> rmap = new HashMap<String, Object>();
        int m = scdhService.edit_poetry(gmap);
        if (m > 0) {
            //如果该诗词大会开启 则其他诗词大会关闭
            if (pstate.equals("0")) {
                scdhService.close_otherpoetry(gmap);
            }
            /*else{
                //计算每个年级前十名的同学 并给与积分
                //查询前十名的同学
                scdhService.set_poetryJf(gmap);
            }*/
            rmap.put("result", true);
            rmap.put("msg", "保存成功!");
            return rmap;
        }
        rmap.put("result", false);
        rmap.put("msg", "添加失败,请联系管理员");
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 诗词关闭且计算积分
     * @time 2019年8月12日11:17:23
     */
    @RequestMapping("close_poetry_pm.html")
    public @ResponseBody
    Map<String, Object> close_poetry_pm(HttpServletRequest req) throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String pid = req.getParameter("pid");
        String totalscore = req.getParameter("totalscore");
        String maxpm = req.getParameter("maxpm");
        gmap.put("pid", pid);
        gmap.put("pstate", "1");

        //关闭诗词大会
        scdhService.edit_poetry(gmap);
        //计算每个年级前十名的同学 并给与积分
        //查询前十名的同学
        gmap.put("totalscore", totalscore);
        gmap.put("maxpm", maxpm);
        scdhService.set_poetryJf(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("result", true);
        rmap.put("msg", "关闭成功!");
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 查询诗词大会图片
     * @time 2019年9月21日21:42:00
     */
    @RequestMapping("getPoetryPic.html")
    @ResponseBody
    public Map<String, Object> getPoetryPic(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("code", "poetry");
        List<Map<String, Object>> list1 = jfychservice.getlunboPic(rmap);
        Map<String, Object> gmap = new HashMap<String, Object>();
        if (list1.size() > 0) {
            gmap = list1.get(0);
        }
        return gmap;
    }


    /**
     * @author 牟明艳
     * @comments 诗词修改
     * @time 2019年8月12日11:17:23
     */
    @RequestMapping("insertPoetryPic.html")
    public @ResponseBody
    boolean insertPoetryPic(HttpServletRequest req) throws Exception {
        Map<String, Object> gmap = new HashMap<String, Object>();
        String imageurl = req.getParameter("imageurl");
        gmap.put("picurl", imageurl);
        gmap.put("code", "poetry");
        gmap.put("codename", "poetry");
        gmap.put("xh", 0);
        jfychservice.delLunboPic(gmap);

        int m = jfychservice.add_picture(gmap);
        if (m > 0) {

            String fileurls = "upload" + "/scdhtop";
            String filepath = req.getServletContext().getRealPath(fileurls);
            File file = new File(filepath);
            if (file.isDirectory()) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    String filed = fileurls + "/" + filelist[i];
                    if (!imageurl.equals(filed)) {
                        File readfile = new File(filepath + "/" + filelist[i]);
                        readfile.delete();
                    }
                }
            }

            return true;
        }
        return false;
    }


    /**
     * @author 牟明艳
     * @comments 分页查询诗词排名
     * @time 2019年10月22日00:14:21
     */
    @RequestMapping("Search_ScdhPm.html")
    @ResponseBody
    public Map<String, Object> Search_ScdhPm(HttpServletRequest request, HttpServletResponse response) {
        String nj = "";
        if (request.getParameter("nj") != null) {
            nj = request.getParameter("nj").toString().trim();
        }
        String bid = "";
        if (request.getParameter("bid") != null) {
            bid = request.getParameter("bid").toString().trim();
        }

        int page = 1;
        if (request.getParameter("_PAGE_NO_") != null) {
            page = Integer.parseInt(request.getParameter("_PAGE_NO_").toString().trim());
        }

        int size = 10;
        if (request.getParameter("_PAGE_SIZE_") != null) {
            size = Integer.parseInt(request.getParameter("_PAGE_SIZE_").toString().trim());
        }
        int start = page * size - size + 1;
        int end = page * size;


        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("nj", nj);
        gmap.put("bid", bid);
        gmap.put("start", start);
        gmap.put("end", end);

        List<Map<String, Object>> list1 = scdhService.Search_ScdhPm_page(gmap);
        int totals = scdhService.Search_ScdhPm_count(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("data", list1);
        rmap.put("totalItems", totals);
        return rmap;
    }


}
